package io.renren.common.utils;

import io.renren.modules.area_region.entity.Feedata;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.phprpc.util.AssocArray;
import org.phprpc.util.Cast;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtils {

	public static String formatMoney(BigDecimal baseFee) {
		baseFee = baseFee.setScale(2, BigDecimal.ROUND_HALF_UP);
		return "¥" + baseFee.toString();
	}

	// 模拟get请求接口返回json数据格式
	public static String doGet(String url) {
		String result = "";
		// 获取httpclient对象
		@SuppressWarnings("deprecation")
		DefaultHttpClient httpClient = new DefaultHttpClient();
		// 获取get请求对象
		HttpGet httpGet = new HttpGet(url);
		try {
			// 发起请求
			HttpResponse response = httpClient.execute(httpGet);
			// 获取响应中的数据
			HttpEntity entity = response.getEntity();
			// 把entity转换成字符串
			result = EntityUtils.toString(entity, "utf-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

 

	// 生成订单号
	public static String createOrderSn() {
		String order_sn = "kz" + System.currentTimeMillis() + (int) (Math.random() * 900 + 100);
		return order_sn;
	}

	public static void serializeFeeData(String fee_data) {
		JSONArray jsonArray = JSONArray.fromObject(fee_data);
		List<Feedata> feeDataList = new ArrayList<>();
		if (jsonArray.size() > 0) {

			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject carDetailObj = jsonArray.getJSONObject(i);
				System.out.println(carDetailObj);
				Feedata feedata = (Feedata) JSONObject.toBean(carDetailObj, Feedata.class);
				System.out.println(feedata.toString());
				feeDataList.add(feedata);
			}
		}
		String feeDataStr = Feedata.serializeToString(feeDataList);
		System.out.println(feeDataStr);

	}

	public static String createSpbillno() {
		String order_sn = "" + System.currentTimeMillis() + (int) (Math.random() * 900 + 100);
		return order_sn;
	}

	public static Integer getCurrentTimeStamp() {
		String time = System.currentTimeMillis()/1000 + "";
		return Integer.parseInt(time);
	}

	public static long timeStampPlus3(Integer time) {
		return Long.valueOf(time + "000");
	}
	/**
	 * 密码加密
	 * @param trim
	 * @param encrypt
	 * @return
	 */
	public static String encrypt(String trim, String encrypt) {
		String base = CommonUtils.md5(trim) + encrypt;
		String md5 = CommonUtils.md5(base);
		return md5;
	}

	// 写一个md5加密的方法
	public static String md5(String plainText) {
		// 定义一个字节数组
		byte[] secretBytes = null;
		try {
			// 生成一个MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 对字符串进行加密
			md.update(plainText.getBytes());
			// 获得加密后的数据
			secretBytes = md.digest();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有md5这个算法！");
		}
		// 将加密后的数据转换为16进制数字
		String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
		// 如果生成数字未满32位，需要前面补0
		for (int i = 0; i < 32 - md5code.length(); i++) {
			md5code = "0" + md5code;
		}
		return md5code;
	}

	public static void main(String[] args) throws IllegalAccessException, UnsupportedEncodingException {
 		// String jsonArrStr="[{\"shipping_id\":\"38\",\"fee\":\"302\",\"time\":\"7-10个工作日\",\"shipping_name\":\"顺丰国际专线\"},{\"shipping_id\":\"13\",\"fee\":\"406\",\"time\":\"7-9个工作日\",\"shipping_name\":\"EMS专线（推荐）\"},{\"shipping_id\":\"44\",\"fee\":\"322\",\"time\":\"20-30个工作日\",\"shipping_name\":\"邮政大包\"}]";
 		
 		 
 		//System.out.println(jsonArrToPhpSerializer(jsonArrStr));
//		
//		List<Map<String, Object>>maplist=jsonArrToMapList(jsonArrStr);
//        for(Map<String,Object> map : maplist){
//        	Feedata map2Bean = SerializeUtil.map2Bean(map, Feedata.class);
//        	Map<String, Object> bean2Map = SerializeUtil.bean2Map(map2Bean);
//        	System.out.println(bean2Map);
//        	String mapToPhpSerializer = SerializeUtil.serialize2(bean2Map);
//        	System.out.println(mapToPhpSerializer);
//        }
//		String s="a:9:{i:0;a:3:{s:11:\"shipping_id\";s:1:\"2\";s:3:\"fee\";s:3:\"408\";s:4:\"time\";s:15:\"2-3个工作日\";}i:1;a:3:{s:11:\"shipping_id\";s:2:\"16\";s:3:\"fee\";s:3:\"502\";s:4:\"time\";s:12:\"3-5工作日\";}i:2;a:3:{s:11:\"shipping_id\";s:2:\"34\";s:3:\"fee\";s:3:\"431\";s:4:\"time\";s:15:\"7-9个工作日\";}i:3;a:3:{s:11:\"shipping_id\";s:2:\"40\";s:3:\"fee\";s:3:\"568\";s:4:\"time\";s:15:\"5-7个工作日\";}i:4;a:3:{s:11:\"shipping_id\";s:2:\"21\";s:3:\"fee\";s:3:\"472\";s:4:\"time\";s:15:\"7-9个工作日\";}i:5;a:3:{s:11:\"shipping_id\";s:2:\"47\";s:3:\"fee\";s:3:\"389\";s:4:\"time\";s:15:\"5-9个工作日\";}i:6;a:3:{s:11:\"shipping_id\";s:2:\"48\";s:3:\"fee\";s:3:\"581\";s:4:\"time\";s:15:\"7-9个工作日\";}i:7;a:3:{s:11:\"shipping_id\";s:2:\"13\";s:3:\"fee\";s:3:\"379\";s:4:\"time\";s:15:\"5-7个工作日\";}i:8;a:3:{s:11:\"shipping_id\";s:2:\"44\";s:3:\"fee\";s:3:\"291\";s:4:\"time\";s:17:\"20-30个工作日\";}}";
//		Object unserialize =PHPSerializer.unserialize(s.getBytes("utf-8"));
//		 
//		//AssocArray array = (AssocArray) unserialize;
//		List<HashMap>aList=(List<HashMap>) unserialize;
//		for (HashMap hashMap : aList) {
//			 
//			System.out.println(hashMap);
//		}
		//String s="a:9:{i:0;a:3:{s:11:\"shipping_id\";s:1:\"2\";s:3:\"fee\";s:3:\"408\";s:4:\"time\";s:15:\"2-3个工作日\";}i:1;a:3:{s:11:\"shipping_id\";s:2:\"16\";s:3:\"fee\";s:3:\"502\";s:4:\"time\";s:12:\"3-5工作日\";}i:2;a:3:{s:11:\"shipping_id\";s:2:\"34\";s:3:\"fee\";s:3:\"431\";s:4:\"time\";s:15:\"7-9个工作日\";}i:3;a:3:{s:11:\"shipping_id\";s:2:\"40\";s:3:\"fee\";s:3:\"568\";s:4:\"time\";s:15:\"5-7个工作日\";}i:4;a:3:{s:11:\"shipping_id\";s:2:\"21\";s:3:\"fee\";s:3:\"472\";s:4:\"time\";s:15:\"7-9个工作日\";}i:5;a:3:{s:11:\"shipping_id\";s:2:\"47\";s:3:\"fee\";s:3:\"389\";s:4:\"time\";s:15:\"5-9个工作日\";}i:6;a:3:{s:11:\"shipping_id\";s:2:\"48\";s:3:\"fee\";s:3:\"581\";s:4:\"time\";s:15:\"7-9个工作日\";}i:7;a:3:{s:11:\"shipping_id\";s:2:\"13\";s:3:\"fee\";s:3:\"379\";s:4:\"time\";s:15:\"5-7个工作日\";}i:8;a:3:{s:11:\"shipping_id\";s:2:\"44\";s:3:\"fee\";s:3:\"291\";s:4:\"time\";s:17:\"20-30个工作日\";}}";
		//System.out.println(phpserialzeToMapList(s));
 		// String s="a:3:{i:0;a:4:{s:11:\"shipping_id\";s:2:\"38\";s:3:\"fee\";s:3:\"302\";s:4:\"time\";s:16:\"7-10个工作日\";s:13:\"shipping_name\";s:18:\"顺丰国际专线\";}i:1;a:4:{s:11:\"shipping_id\";s:2:\"13\";s:3:\"fee\";s:3:\"406\";s:4:\"time\";s:15:\"7-9个工作日\";s:13:\"shipping_name\";s:21:\"EMS专线（推荐）\";}i:2;a:4:{s:11:\"shipping_id\";s:1:\"2\";s:3:\"fee\";s:3:\"355\";s:4:\"time\";s:15:\"3-5个工作日\";s:13:\"shipping_name\";s:3:\"DHL\";}}";
 		// System.out.println(phpserialzeToMapList(s));
		 

	}
	/**
	 * phpserialzeToMapList
	 * @param phpserialze
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List<Map> phpserialzeToMapList(String phpserialze) {
		Object unserialize = null;
		try {
			unserialize = PHPSerializer.unserialize(phpserialze.getBytes("utf-8"));
		} catch (IllegalAccessException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		 
		@SuppressWarnings("unchecked")
		List<Map>aList=(List<Map>) unserialize;
		return aList;
	}
	/**
	 * jsonArr转phpserialze
	 * @param mapImage
	 * @return
	 */
	public static String jsonArrToPhpSerializer(String jsonArrStr) {
		List<Map<String, Object>> jsonArrToMapList2 = jsonArrToMapList2(jsonArrStr);
 		String serialize3 = SerializeUtil.serialize3(jsonArrToMapList2);
        return serialize3;
	}
	public static List<Map<String, Object>> jsonArrToMapList2(String jsonArrStr) {
		List<Map<String, Object>>newMapList=new ArrayList<>();
		List<Map<String, Object>>maplist=jsonArrToMapList(jsonArrStr);
        for(Map<String,Object> map : maplist){
        	Feedata map2Bean = SerializeUtil.map2Bean(map, Feedata.class);
        	Map<String, Object> bean2Map = SerializeUtil.bean2Map(map2Bean);
        	newMapList.add(bean2Map);
        	 
        }
        return newMapList;
	}
 
	/**
	 * json字符串数组转List
	 * @param json
	 * @return
	 */
	public static List<Map<String,Object>> jsonArrToMapList(String json) {
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> mapList = (List<Map<String,Object>>) com.alibaba.fastjson.JSONArray.parse(json);
		return mapList;
	}

	public static String timeStampToTime(Long addTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 设置格式
		String timeText = format.format(new Date(addTime));
		return timeText;
	}

	public static Integer timeStrToTimeStamp3(String addTimeStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(addTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String timestamp = String.valueOf(date.getTime() / 1000);
		return Integer.valueOf(timestamp);
	}

	public static BigDecimal stringTwoBigDecimal(String total_fee) {
		BigDecimal b = new BigDecimal(total_fee); 
		b=b.setScale(2, BigDecimal.ROUND_HALF_UP); //四舍五入
		return b;
	}

	public static String createUserName() {
		String randomcode2 = "";  
	    String model = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
	    char[] m = model.toCharArray();  

	    for (int j=0;j<6 ;j++ )  
	    {  
	        char c = m[(int)(Math.random()*26)];  
	        randomcode2 = randomcode2 + c;  
	    }  

	    System.out.println(randomcode2); 
		return randomcode2;
	}
	//用字符数组的方式随机  

	public static String dateToString(Date expire) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(expire);
		return dateString;
	}
     
	 

}
